<?php
namespace app\admin\model;


class MerchSetplus
{
    protected $cardno = '200031011400278608';//虚拟账号,非商户号(18位的数字，联系业务获取)
    protected $key = '10787E92FA08E2B3726D8C687B946A97';//代付密钥,非交易密钥(32位的字母+数字，联系业务获取)
    protected $urlPrefix = 'http://settle.setplus.cn/';//接口地址(联系业务获取)

    //余额查询接口
    public function balance()
    {
        $url = $this->urlPrefix . 'balance.do';

        $request = array(
            //必填项
            'cardno' => $this->cardno,//虚拟账号
        );

        //拼接上送的数据
        $src='';

        foreach ($request as $x => $x_value){
            if ($x_value != null){
                $src = $src . $x . "=" . iconv('UTF-8','GBK//IGNORE',$x_value)."&"; //采用本方法进行GBK编码
            }
        }
//        dd($src . 'key=' . $this->key);
        $md5 = md5($src . 'key=' . $this->key);
//        echo "签名结果:<br/>".$md5."<br/>";

        //数据通讯
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $src . 'signature' . '=' . $md5);
        $response = curl_exec($curl);
        curl_close($curl);

        $res = iconv('GBK//IGNORE', 'UTF-8', $response);
        $res = json_decode($res, true);
dd($res);
        return $res;

    }

    //商家转账接口
    public function virtPay($info, $amount, $traceno)
    {
        $url = $this->urlPrefix . 'virtPay.do';

        $request = array(
            //必填项
            'cardno'    => $this->cardno,//虚拟账号
            'traceno'   => $traceno,//自定义流水号
            'amount'    => $amount,//'0.01',//提现金额，以元为单位
            'accountno' => $info['card_no'],//'6228480626748032275',//银行卡卡号
            'mobile' => '13800000000',//'18028432514',//银行卡预留手机号
            'bankno' => '000000000000',//'103100000026',//银行卡卡号
        );

        $request2 = array(
            //必填项
            'cardno'    => $this->cardno,//虚拟账号
            'traceno'   => $traceno,//自定义流水号
            'amount'    => $amount,//'0.01',//提现金额，以元为单位
            'accountno' => $info['card_no'],//'6228480626748032275',//银行卡卡号

            'accountName' => $info['real_name'],//'庄延超',//银行卡姓名

            'mobile' => '13800000000',//'18028432514',//银行卡预留手机号

            'certno' => '000000000000000000',//'44080319870804393X',//身份证号

            'bankno' => '000000000000',//'103100000026',//银行卡卡号

            'bankName' => '中国银联',//'中国农业银行股份有限公司忻州分行',//银行名称
            'bankType' => '中国银联',//'中国农业银行',//银行名称
            'remark' => '绿色积分兑换',//备注信息
        );

        //拼接上送的数据
        $src='';

        foreach ($request as $x => $x_value){
            if ($x_value != null){
                $src = $src . $x . "=" . iconv('UTF-8','GBK//IGNORE',$x_value) . "&"; //采用本方法进行GBK编码
            }
        }
        //dd($src . 'key=' . $this->key);
        $md5 = md5($src . 'key=' . $this->key);
//        echo "签名结果:<br/>".$md5."<br/>";

        $src2='';
        foreach ($request2 as $x => $x_value){
            if ($x_value != null){
                $src2 = $src2 . $x . "=" . iconv('UTF-8','GBK//IGNORE',$x_value) . "&"; //采用本方法进行GBK编码
            }
        }
        //dd($src2 . 'signature' . '=' . $md5);
        //数据通讯
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $src2 . 'signature' . '=' . $md5);
        $response = curl_exec($curl);
        curl_close($curl);

        $res = iconv('GBK//IGNORE', 'UTF-8', $response);
        $res = json_decode($res, true);
        //dd($res);
        return $res;
    }


    //转账查询接口
    public function checkWithdraw($traceno)
    {
        $url = $this->urlPrefix . 'virtOrder.do';

        $request = array(
            //必填项
            'cardno'    => $this->cardno,//虚拟账号
            'traceno'   => $traceno,//自定义流水号
        );

        //拼接上送的数据
        $src='';

        foreach ($request as $x => $x_value){
            if ($x_value != null){
                $src = $src . $x . "=" . iconv('UTF-8','GBK//IGNORE',$x_value)."&"; //采用本方法进行GBK编码
            }
        }
        //dd($src . 'key=' . $this->key);
        $md5 = md5($src . 'key=' . $this->key);
//        echo "签名结果:<br/>".$md5."<br/>";

        //数据通讯
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $src . 'signature' . '=' . $md5);
        $response = curl_exec($curl);
        curl_close($curl);

        $res = iconv('GBK//IGNORE', 'UTF-8', $response);
        $res = json_decode($res, true);

        return $res;
    }
}